package com.zhiyou.aifeng.viewmodel

import com.lzg.extend.BaseResponse
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.zhiyou.aifeng.common_ui.model.BaseUrl
import com.zhiyou.aifeng.common_ui.model.CommonData
import com.zhiyou.aifeng.common_ui.model.CommonModel
import com.zhiyou.aifeng.common_ui.utils.applyCompletableSchedulers
import com.zhiyou.aifeng.common_ui.utils.applyFlowableSchedulers
import com.zhiyou.aifeng.common_ui.utils.subscribeByFinally
import com.zhiyou.aifeng.common_ui.viewmodel.BaseViewModel
import io.reactivex.Completable
import java.util.concurrent.TimeUnit

class RepairViewModel : BaseViewModel() {

    fun cancelRequest() = mDisposables.clear()

    fun delay(
        time: Long = 300,
        onResult: () -> Unit = {}
    ) =
        Completable.timer(time, TimeUnit.MILLISECONDS)
            .compose(applyCompletableSchedulers())
            .subscribe { onResult() }

    fun getRepairList(page: Int) =
        OkGo.post<BaseResponse<CommonModel>>(BaseUrl.repairList)
            .params(
                getParams(
                    "page" to page,
                    "pageSize" to 10
                )
            )
            .converter(object : JacksonConvert<BaseResponse<CommonModel>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.results }

    fun getMsgList(
        userId: String,
        index: Int,
        onSuccess: (ArrayList<CommonData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) {
        mDisposables.add(
            OkGo.post<BaseResponse<CommonModel>>(BaseUrl.msgList)
                .params(
                    getParams(
                        "uid" to userId,
                        "type" to 2,
                        "page" to index
                    )
                )
                .converter(object : JacksonConvert<BaseResponse<CommonModel>>() {})
                .adapt(FlowableBody())
                .compose(applyFlowableSchedulers())
                .map { it.data.list }
                .subscribeByFinally(
                    event = this,
                    onSuccess = { onSuccess(it) },
                    onFinally = { onFinally() }
                )
        )
    }

    fun getSystemList(
        userId: String,
        index: Int,
        onSuccess: (ArrayList<CommonData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) {
        mDisposables.add(
            OkGo.post<BaseResponse<CommonModel>>(BaseUrl.sysList)
                .params(
                    getParams(
                        "uid" to userId,
                        "role" to 2,
                        "page" to index
                    )
                )
                .converter(object : JacksonConvert<BaseResponse<CommonModel>>() {})
                .adapt(FlowableBody())
                .compose(applyFlowableSchedulers())
                .map { it.data.list }
                .subscribeByFinally(
                    event = this,
                    onSuccess = { onSuccess(it) },
                    onFinally = { onFinally() }
                )
        )
    }

}